package com.yuong.demo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import com.yuong.demo.DensityUtil;

import androidx.annotation.Nullable;

/**
 * 〈进度View〉
 *
 * @author yuandong
 * @create 2021/4/25
 * @since 1.0.0
 */
public class ProgressView extends View {

    private int width;
    private int height;
    private float centerX, centerY;
    private float radius;
    private float paddingTop;
    private float paddingBottom;
    private float circleWidth;
    private float arcWidth;
    private float lineWidth;
    private Paint circlePaint;
    private Paint arcPaint;

    private Paint valuePaint;
    private Paint titlePaint;
    private Paint linePaint;
    private Paint percentPaint;
    private float valueTextSize;
    private float titleTextSize;
    private float percentTextSize;

    private Paint.FontMetrics percentFontMetrics;

    private int[] colors = new int[]{Color.parseColor("#92D1FF"), Color.parseColor("#E8B6ED")};

    private int sweepAngle = 250;

    public ProgressView(Context context) {
        this(context, null);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paddingTop = DensityUtil.dp2px(getContext(), 20);
        paddingBottom = DensityUtil.dp2px(getContext(), 20);
        circleWidth = DensityUtil.dp2px(getContext(), 12);
        arcWidth = DensityUtil.dp2px(getContext(), 24);
        lineWidth = DensityUtil.dp2px(getContext(), 1);
        valueTextSize = DensityUtil.dp2px(getContext(), 26);
        titleTextSize = DensityUtil.dp2px(getContext(), 13);
        percentTextSize = DensityUtil.dp2px(getContext(), 12);

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(circleWidth);
        circlePaint.setColor(Color.parseColor("#ECECEC"));

        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setStrokeWidth(arcWidth);

        titlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        titlePaint.setTextSize(titleTextSize);
        titlePaint.setColor(Color.parseColor("#666666"));
        titlePaint.setTextAlign(Paint.Align.CENTER);

        valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        valuePaint.setTextSize(valueTextSize);
        valuePaint.setColor(Color.parseColor("#141414"));
        valuePaint.setTextAlign(Paint.Align.CENTER);

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setStrokeWidth(lineWidth);

        percentPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        percentPaint.setTextAlign(Paint.Align.LEFT);
        percentPaint.setTextSize(percentTextSize);
        percentPaint.setColor(Color.parseColor("#666666"));
        percentPaint.setTextAlign(Paint.Align.CENTER);
        percentFontMetrics = percentPaint.getFontMetrics();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        radius = width * 0.45f * 0.5f;//直径为view宽度的0.45
        centerX = 0.5f * width;//圆心x
        centerY = paddingTop + radius;
        height = (int) (width * 0.45f + paddingTop + paddingBottom);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画默认圆环
        float defaultCircleRadius = radius - arcWidth + circleWidth * 0.5f;
        canvas.drawCircle(centerX, centerY, defaultCircleRadius, circlePaint);

        //画圆弧
        float left = centerX - radius + arcWidth * 0.5f;
        float right = centerX + radius - arcWidth * 0.5f;
        float top = centerY - radius + arcWidth * 0.5f;
        float bottom = centerY + radius - arcWidth * 0.5f;

        RectF oval = new RectF(left, top, right, bottom);

        SweepGradient sweepGradient = new SweepGradient(centerX, centerY, colors, null);
        arcPaint.setShader(sweepGradient);
        canvas.rotate(-90, centerX, centerY);//以圆心旋转的
        canvas.drawArc(oval, 0, sweepAngle, false, arcPaint);
        canvas.rotate(90, centerX, centerY);

        //画标题
        float titleY = centerY + titleTextSize + DensityUtil.dp2px(getContext(), 5);
        canvas.drawText("订单量", centerX, titleY, titlePaint);

        //画数值
        titleY = centerY - DensityUtil.dp2px(getContext(), 5);
        canvas.drawText("200", centerX, titleY, valuePaint);

        //画线
        drawLine(canvas);
    }

    //绘制右侧线
    private void drawLine(Canvas canvas) {
        float length = DensityUtil.dp2px(getContext(), 100);//线的长度
        double angle = Math.PI * sweepAngle / 360d;//取旋转弧度的一半位置的点画线
        float x = (float) (centerX + Math.sin(angle) * radius);
        float y = (float) (centerY - Math.cos(angle) * radius);
        linePaint.setColor(Color.parseColor("#ACC4FF"));
        canvas.drawLine(x, y, x + length, y, linePaint);

        float x2 = (float) (centerX - Math.sin(angle) * (radius - arcWidth * 0.5f));
        float y2 = (float) (centerY + Math.cos(angle) * (radius - arcWidth * 0.5f));
        linePaint.setColor(Color.parseColor("#C3C3C3"));
        canvas.drawLine(x2, y2, x2 - length, y2, linePaint);

        String value1 = "168（77%）";
        percentPaint.measureText(value1);
        canvas.drawText(value1, x + length / 2, y - percentFontMetrics.bottom, percentPaint);

        String value2 = "32（33%）";
        percentPaint.measureText(value2);
        canvas.drawText(value2, x2 - length / 2, y2 - percentFontMetrics.bottom, percentPaint);
    }
}